<template>
	<el-container>
		<el-header style="border-bottom: none;height: 50px;">
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'othersz.add'" ></el-button>
                <sc-export-button :auth="'othersz.export'" :apiObj="apiObj" :search="search" :selection="selection"></sc-export-button>
                <!-- <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'othersz.delete'" ></el-button> -->
                <div class="finish">
                    <div class="finishNum">总收入:<div class="text">{{ total_stock.total_income }}</div></div>
                    <div class="finishNum">总支出:<div class="text">{{ total_stock.total_expense }}</div></div>
                    <div class="finishNum">净收入:<div class="text">{{ total_stock.net_income }}</div></div>
                </div>
            </div>
		</el-header>
		<el-header>
            <div class="left-panel">
            </div>
			<div class="right-panel">
				<div class="right-panel-search">
                    <el-select v-model="search.account_id" placeholder="账户" filterable>
                    <el-option v-for="item in accountList" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
					<el-select v-model="search.inout_type" clearable placeholder="收支类型">
                        <el-option label="收入" :value="1" />
                        <el-option label="支出" :value="2" />
                    </el-select>
                    <el-select v-model="search.inout_id" placeholder="收支事项" filterable clearable>
                        <el-option v-for="item in matterList" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select>
					<el-input v-model="search.remark" placeholder="备注" clearable @keyup.enter="upsearch"></el-input>
                    <el-date-picker v-model="search.date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					<el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border :params="search" tableName="otherTable" :column="otherColumn" @cell-click="cellClick" @setExtend="setExtend">
				<el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
				<el-table-column type="index" label="序号" fixed align="center" width="60"></el-table-column>
                <template #inout_type="{row}">
                    <el-tag :type="row.inout_type == 1 ? 'success' : 'danger'">
                        {{row.inout_type == 1 ? '收入' : '支出'}}
                    </el-tag>
                </template>
                <template #image="scope">
                    <el-image v-if="scope.row.image" style="width: 50px; height: 50px; border-radius: 5px;" :src="scope.row.image" :preview-src-list="[scope.row.image]" preview-teleported="true" hide-on-click-modal align="center"></el-image>
                </template>
                <template #account="scope">
                    {{`【${scope.row.account?.account_bank}】${scope.row.account?.account && /^\d+$/.test(scope.row.account.account) ? scope.row.account.account.slice(-4) : ''}`}}
                </template>
                <template #operation="scope">
                    <div class="buttonFlex">
                        <el-button type="primary" size="small" @click="table_edit(scope.row)" v-auth="'othersz.edit'" v-if="scope.row.status != 1">编辑</el-button>
                        <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                            <template #reference>
                                <el-button type="danger" size="small" v-auth="'othersz.delete'" v-if="scope.row.status != 1">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </div>
                </template>
                <!-- <el-table-column label="日期" prop="date" align="center" width="200"></el-table-column>
                <el-table-column label="类型" prop="inout_type" align="center" width="100">
                    <template #default="{row}">
                        <el-tag :type="row.inout_type == 1 ? 'success' : 'danger'">
                            {{row.inout_type == 1 ? '收入' : '支出'}}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="事项" prop="inout.name" align="center" width="150"></el-table-column>
                <el-table-column label="金额" prop="real_price" align="center" width="100"></el-table-column>
                <el-table-column label="图片" prop="image" align="center" width="100">
                    <template #default="scope">
                        <el-image v-if="scope.row.image" style="width: 50px; height: 50px; border-radius: 5px;" :src="scope.row.image" :preview-src-list="[scope.row.image]" preview-teleported="true" hide-on-click-modal align="center"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="对应账户" prop="account" align="center" width="150">
                    <template #default="scope">
                        {{`【${scope.row.account?.account_bank}】${scope.row.account?.account && /^\d+$/.test(scope.row.account.account) ? scope.row.account.account.slice(-4) : ''}`}}
                    </template>
                </el-table-column>
                <el-table-column label="备注" prop="remark" align="center"></el-table-column>
                <el-table-column label="业务员" prop="creator_name" align="center" width="100"></el-table-column>
				<el-table-column label="操作" align="center" width="150">
					<template #default="scope">
                        <div class="buttonFlex">
                            <el-button type="primary" size="small" @click="table_edit(scope.row)" v-auth="'othersz.edit'" v-if="scope.row.status != 1">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button type="danger" size="small" v-auth="'othersz.delete'" v-if="scope.row.status != 1">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </div>
					</template>
				</el-table-column> -->
			</scTable>
		</el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
</template>

<script>
import saveDialog from './save'

export default {
	name: '其他收支列表',
	components: {
		saveDialog,
	},
	data() {
		return {
			dialog: {
				save: false,
			},
            apiObj: this.$API.business.finance.collect.record,
			selection: [],
			search: {
                account_id: null,
                inout_id: null,
                remark: null,
                type: 34,
                date: [this.moment().startOf('month').format('YYYY-MM-DD'), this.moment().endOf('month').format('YYYY-MM-DD')],
			},
            accountList: [],
            matterList: [],
            total_stock: {
                total_income: 0,
                total_expense: 0,
                net_income: 0,
            },
            otherColumn: [
                { label: "日期", prop: "date", width: "200",align: 'left'},
                { label: "类型", prop: "inout_type", width: "100",align: 'center'},
                { label: "事项", prop: "inout.name", width: "150",align: 'center'},
                { label: "金额", prop: "real_price", width: "100",align: 'center'},
                { label: "图片", prop: "image", width: "100",align: 'center'},
                { label: "对应账户", prop: "account", width: "150",align: 'center'},
                { label: "备注", prop: "remark",align: 'center'},
                { label: "业务员", prop: "creator_name", width: "100",align: 'center'},
                { label: "操作", prop: "operation", width: "150",fixed: 'right'},
            ],
		}
	},
	mounted() {
        this.getAccount();
        this.getMatter();
	},
	methods: {
        setExtend(data){
            this.total_stock = data
        },
        cellClick(row, column){
            let is_search = false;
            // for (const key in this.search) {
            //     this.search[key] = null;
            // }
            if(column.property == 'inout.name'){
                this.search.inout_id = row.inout_id
                is_search = true;
            }
            if(is_search){ 
                this.upsearch();
            }
        },
		///添加
        add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open()
            })
        },
        //编辑
        table_edit(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit').setData(row)
            })
        },
        //查看
        table_show(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('show').setData(row)
            })
        },
        //删除
        async table_del(row) {
            var reqData = { id: row.id }
            var res = await this.$API.business.finance.other.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        async table_del_all(delete_ids) {
            var reqData = { id: delete_ids }
            var res = await this.$API.business.finance.other.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        //批量删除
        async batch_del() {
            var delete_ids = [];
            this.selection.forEach((item) => {
                delete_ids.push(item.id)
            })

            this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
                type: 'warning'
            }).then(() => {
                this.table_del_all(delete_ids)
            }).catch(() => {

            })
        },
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		refresh() {
			for (const key in this.search) {
				this.search[key] = null;
			}
            this.search.type = 34
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
        // 获取账户
        async getAccount() {
            var res = await this.$API.basic.account.all.get();
            const array = []
            res.data.map(item=>{
                array.push({
                    id: item.id,
                    name: `${item.account_bank} ${/^\d+$/.test(item.account) ? `【${item.account.slice(-4)}】` : ''}`
                })
            })
            this.accountList = array
        },
        // 收支事项
        async getMatter() {
            var res = await this.$API.basic.matter.all.get();
            this.matterList = res.data
        },
	}
}
</script>

<style></style>
